#!/bin/sh
#参数1 应用程序名称，如foxit-pcs-web
#参数2 jar包的版本，如1.0
#参数3 原始文件存放目录
#参数4 存放目录，如/usr/share/tomcat/apache-tomcat-7.0.37/temp

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]
then
	echo "Parameters Fail....."
	exit 1;
fi

#mount /s3目录
#返回值：成功返回0，失败返回非0
function MountNFSToLocalDirS3()
{
	local retVal=1
	local mountNfsStr=$(mount | grep "puppet.foxitcloud.com:/s3")
	if [ -z "$mountNfsStr" ]
	then
		local nfsDir=$(showmount -e puppet.foxitcloud.com | grep "/s3" | grep -v "public" | cut -d ' ' -f 1)
		if [ -n "$nfsDir" ]
		then
			if [ ! -d "/s3" ]
			then
				mkdir /s3
			fi
			
			mount puppet.foxitcloud.com:${nfsDir} /s3
			retVal=$?
		fi
	else
		retVal=0
	fi
	
	return ${retVal};
}


MountNFSToLocalDirS3
if [ $? -ne 0 ]
then
	echo "mount S3Dir faile..."
	exit 1;
fi

#程序参数定义
APP_FILE_NAME=$1
WAR_VERSION=$2
WAR_FILE_SRC_DIR=$3
WAR_FILE_DIR=$4
WAR_FILE_NAME="${APP_FILE_NAME}-${WAR_VERSION}.jar"

RetValue=0

rm -rf ${WAR_FILE_DIR}/${APP_FILE_NAME}-*.jar
cp ${WAR_FILE_SRC_DIR}/$WAR_FILE_NAME  $WAR_FILE_DIR

#备份webapp war包到本地文件系统中，用于回滚
WEBAPP_BACKUP_VERSION_DIR="/usr/share/backupFoxitCloud/${APP_FILE_NAME}/${WAR_VERSION}"

if [ ! -d "$WEBAPP_BACKUP_VERSION_DIR" ];then 
	mkdir -p "$WEBAPP_BACKUP_VERSION_DIR" 
fi

if [ -f "$WAR_FILE_DIR/$WAR_FILE_NAME" ]; then
	cp -rf $WAR_FILE_DIR/$WAR_FILE_NAME $WEBAPP_BACKUP_VERSION_DIR
	retValue=$?
	if [ $retValue != 0 ]; then 
		echo "cp -rf $WAR_FILE_DIR/$WAR_FILE_NAME $WEBAPP_BACKUP_VERSION_DIR error."
	fi
else
	retValue=1
	echo "file($WAR_FILE_DIR/$WAR_FILE_NAME) not exist."
fi

echo "end deployJar:$APP_FILE_NAME"
exit $RetValue;
